package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.base.util.ViewUtils;

/**
 * Created by Roy
 * Date: 2017/4/12
 * @author zhenhua
 */

public class HomeStatusView extends RelativeLayout {

    private ProgressBar progressBar;

    private LinearLayout llStatus;
    private ImageButton imgError;
    private TextView tvStatus;
    private Button btnRefresh;

    public static final int StatusLoading = 0;
    public static final int StatusEmpty = 1;
    public static final int StatusError = 2;
    public static final int StatusDone = 3;

    private onActionClickListener onActionClickListener;

    public interface onActionClickListener {
        void onRefreshClick();
    }

    public HomeStatusView(Context context, onActionClickListener onActionClickListener) {
        super(context);

        this.onActionClickListener = onActionClickListener;
        LayoutInflater.from(getContext()).inflate(R.layout.view_home_load_state, this);

        initView();
        initListener();
    }

    private void initView() {
        progressBar = (ProgressBar) findViewById(R.id.loading_state_progressBar);

        llStatus = (LinearLayout) findViewById(R.id.ll_status);
        imgError = (ImageButton) findViewById(R.id.img_error);
        tvStatus = (TextView) findViewById(R.id.tv_status);
        btnRefresh = (Button) findViewById(R.id.btn_refresh);
    }

    private void initListener() {
        btnRefresh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onActionClickListener != null)
                    onActionClickListener.onRefreshClick();
            }
        });
    }

    /* 设置计划书列表状态 */
    public void setStatus(int status) {
        switch (status) {
            case StatusLoading:
                ViewUtils.setHeight(this, getContext().getResources().getDimensionPixelOffset(R.dimen.dp_400));

                llStatus.setVisibility(GONE);
                progressBar.setVisibility(VISIBLE);
                btnRefresh.setVisibility(GONE);
                break;
            case StatusEmpty:
                ViewUtils.setHeight(this, getContext().getResources().getDimensionPixelOffset(R.dimen.dp_400));

                llStatus.setVisibility(VISIBLE);
                progressBar.setVisibility(GONE);

                imgError.setVisibility(GONE);
                btnRefresh.setVisibility(GONE);
                tvStatus.setText(getContext().getString(R.string.home_no_plan));
                break;
            case StatusError:
                ViewUtils.setHeight(this, getContext().getResources().getDimensionPixelOffset(R.dimen.dp_400));

                llStatus.setVisibility(VISIBLE);
                progressBar.setVisibility(GONE);

                imgError.setVisibility(VISIBLE);
                btnRefresh.setVisibility(VISIBLE);
                tvStatus.setText(getContext().getString(R.string.loading_fail_for_show));
                break;
            case StatusDone:
                ViewUtils.setHeight(this, 0);

                llStatus.setVisibility(GONE);
                progressBar.setVisibility(GONE);
                break;
        }
    }
}
